﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.Presenter.RequestVoBo;
using System.Collections.Generic;
using Microsoft.Office.Server.UserProfiles;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using System.Web;
using System.Web.UI.WebControls;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.DAL.DTOs;

namespace MS.InformationManagement.WP.Layouts.MS.InformationManagement.WP
{
    public partial class RequestVoBo : BaseApplicationPage, IRequestVoBoView
    {

        #region fields

        RequestVoBoPresenter _presenter;

        #endregion

        #region Properties

        public Dictionary<string, string> SelectedFilesInfo { get; set; }

        public string RequestMessage
        {
            get { return txtObservacion.Text; }
        }

        public int[] SelectedIds 
        {
            get 
            {
                string[] ids = Page.Request.QueryString["itemsID"].Split('|');                
                int[] selectedIds = new int[ids.Length];
                for(int i=0; i<ids.Length-1; i++)
                {
                    selectedIds[i] = int.Parse(ids[i]);
                }
                return selectedIds;
            }  
        }
        public string TargetUser 
        {
            get { return ddlDestinatario.SelectedValue; } 
        }
        public List<UserDTO> DepartmentUsers { get; set; }
        public Guid SourceList
        {
            get { return new Guid(Page.Request.QueryString["lstID"]); }
        }
        #endregion

        #region Form Events

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _presenter = new RequestVoBoPresenter(this);
                if (IsPostBack) return;
                _presenter.HandleLoad();
            }
            catch (Exception ex)
            {
                WebControl[] controls = new WebControl[]{ ddlDestinatario, btnCancelar , btnGuardar };
                HandleError(ex,controls,true);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            txtObservacion.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            //txtObservacion.RegularExpression(RegularExpresions.EXP_MAXLENGTH.Replace("#","1000"), string.Format(ExceptionConstants.ERROR_MAX_LENGTH,"1000"), AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
        }

        protected void SaveAction(object sender, EventArgs e)
        {           
            try
            {
                _presenter.RequestVoBo();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void CloseAction(object sender, EventArgs e)
        {
            string isDlg = Request.QueryString.Get("IsDlg");
            if (!string.IsNullOrEmpty(isDlg) && isDlg.Equals("1")) { this.CloseWindow(); }
            else { this.GoCurrentUserHome(); }            
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            ddlDestinatario.DataSource = DepartmentUsers;
            ddlDestinatario.DataBind();
            rptDocumentos.DataSource = SelectedFilesInfo;
            rptDocumentos.DataBind();
        }

        public void Success()
        {
            WebControl[] controls = new WebControl[] { txtObservacion, ddlDestinatario, btnCancelar, btnGuardar };
            HandleSuccessRedirect("El documento ha sido enviado para VB exitosamente", controls, true, true);
        }

        #endregion

    }
}
